<template>
  <div class="ignore-article">
    <div class="article-content">
      <div class="article-content-head">
        <div class="article-content-avatar"><img :src="data.avatar" alt="" /></div>
        <div>
          <span> {{ data.author }}</span>
          <span class="article-content-title">{{ data.creatTime }}</span>
        </div>
      </div>
      <div class="article-content-main" @click="handleClickShowArticleModal">
        <span>{{ '#' + data.title }}</span>
        <span class="article-content-text">{{ data.content }}</span>
        <div class="article-content-pic">
          <template v-for="(item, index) in data.pic" :key="index">
            <img :src="item" />
          </template>
        </div>
      </div>
      <div class="article-footer">
        <div @click="handleLike" :class="[isLike ? 'article-footer-islike' : '']"><span
            class="iconfont icon-icon-test2"></span>
          点赞:{{ likeNum }}</div>
        <div><span class="iconfont icon-icon-test"></span>评论:{{ data.commentNum }}</div>
        <div @click="handleCollect" :class="[isCollect ? 'article-footer-iscollect' : '']"><span
            class="iconfont icon-icon-test3"></span>收藏:{{
                data.collectNum
            }}</div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, Ref } from "vue"
import { getLikeNum, likeArticle, delLikeArticle } from '@/api/article'
import { userStore } from "@/store/user"

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})
const userInfo = localStorage.getItem('userInf0')
const isLike = ref(false)
const isCollect = ref(false)
const likeNum = ref(0)
const user_store = userStore()
// 获取点赞信息
const u_id = user_store.$state.userInfo._id
const onGetLikeNum = async () => {
  const params = {
    article_id: props.data._id
  }
  const res = await getLikeNum(params)
  res.data.result!.forEach((item, index) => {
    if (item.u_id === u_id) {
      isLike.value = true
    }
  })
  likeNum.value = res.data.result!.length
}

// 弹出框
const handleClickShowArticleModal = () => {
  ElMessageBox.alert(props.data.content, props.data.title, {
    confirmButtonText: '关闭',
    customStyle: {
      width: '80%',
    }
  })
}

// 点赞/取消点赞
const handleLike = async () => {
  const token = localStorage.getItem('token')
  if (!token) {
    return ElMessage('请先登入')
  }
  const params = {
    u_id: u_id,
    article_id: props.data._id
  }
  isLike.value ? [delLikeArticle(params).then(res => {
    if (res) {
      likeNum.value--
    }
  })] : [likeArticle(params).then(res => {
    if (res) {
      likeNum.value++
    }
  })]
  isLike.value = !isLike.value
}

const handleCollect = () => {
  isCollect.value = !isCollect.value
}
defineExpose({ onGetLikeNum })
</script>

<style scoped lang="less">
.article {
  &-content {
    margin-top: 80px;
    padding: 15px;

    &-head {
      display: flex;
      align-items: center;

      >div {
        >span {
          display: block;
          line-height: 40px;
        }

        &:last-child {
          margin-left: 20px;
        }
      }
    }

    &-avatar {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      overflow: hidden;

      >img {
        width: 100%;
      }
    }

    &-title {
      color: #b9c8dc;
    }

    &-main {
      margin-top: 30px;

      >span {
        &:first-child {
          color: #20c98a;
          margin-right: 20px;
        }

        font-size: 30px;
      }

      &::-webkit-scrollbar {
        display: none;
      }
    }

    &-text {
      display: -webkit-box;
      overflow: hidden;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
      text-overflow: ellipsis;
      margin: 20px 0;
    }

    &-pic {
      display: flex;
      justify-content: space-around;
      flex-wrap: wrap;

      >img {
        width: 45%;
        margin-top: 10px;
      }
    }
  }

  &-footer {
    display: flex;
    justify-content: space-between;
    margin-top: 20px;

    &-islike {
      >span {
        color: red;
      }
    }

    &-iscollect {
      >span {
        color: yellow;
      }
    }
  }
}
</style>